import { User } from '@libs/db/models/user.model';
import { Body, Controller, Get, Patch, Req, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { ReturnModelType } from '@typegoose/typegoose';
import { InjectModel } from 'nestjs-typegoose';

@ApiTags('用户信息操作')
@Controller('users')
export class UsersController {
  constructor(
    @InjectModel(User) private readonly userModel: ReturnModelType<typeof User>,
  ) {}

  @ApiOperation({ summary: '获取用户信息' })
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @Get('info')
  findOne(@Req() request: any) {
    return request.user;
  }

  @ApiOperation({ summary: '更新用户信息' })
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @Patch('edit')
  updateOne(@Req() request: any, @Body() user: User) {
    const { _id } = request.user;
    return this.userModel.updateOne({ _id }, user);
  }
}
